package module.map.propertys
{
	import drongo.desktop.utils.UtilsFile;
	import drongo.model.core.IProperty;
	import drongo.model.events.ModelEvent;
	import drongo.model.values.CollectionValue;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	
	import common.view.FGUIMediator;
	import common.window.AlertDialog;
	
	import module.map.Operations;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	import module.map.operations.propertys.datas.PropertyChangeData;
	
	import ui.Map.UI_PathPropertyView;
	
	public class PathPropertyViewMediator extends FGUIMediator
	{
		[Inject]
		public var view:UI_PathPropertyView;
		[Inject]
		public var project:ProjectModel;
		
		private var data:CollectionValue;
		private var property:IProperty;
		public function PathPropertyViewMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.data=this.view.data.data;
			this.property=this.view.data.property;
	
			this.RefreshView();
			this.AddEvents();
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function RefreshView():void{
			this.view.m_nameTxt.text=this.property.GetKey()+":";
			this.view.m_valueTxt.text=this.property.GetValue();
		}
		
		private function AddEvents():void{
			this.view.m_selectButton.addClickListener(this.SelectButtonClick);
			this.property.addEventListener(ModelEvent.VALUE_CHANGED,this.PropertyUpdateHandler);
		}
		
		private function RemoveEvents():void{
			this.view.m_selectButton.removeClickListener(this.SelectButtonClick);
			this.property.removeEventListener(ModelEvent.VALUE_CHANGED,this.PropertyUpdateHandler);
		}
		
		private function SelectButtonClick(e:Event):void{
			UtilsFile.browseForOpen("选择需要图片",[new FileFilter("image","*.png;*.jpg")],this.SelectCallBack);
		}
		
		private function SelectCallBack(file:File):void{
			var mapID:String=this.project.currentMap.Get(Propertys.ID).GetValue();
			
			var srcFile:File=file;
			if(!srcFile.exists){
				AlertDialog.open(this.view.root,"图片不存在！");
			}
			//相对路径
			var imagePath:String;
			var mapDic:String=this.project.imagesFile.nativePath+File.separator
			//如果选择的图片不在工程目录的maps的mapID文件夹下
			if(srcFile.nativePath.indexOf(mapDic)<0){
				var targetFile:File=new File(this.project.imagesFile.nativePath+File.separator+srcFile.name);
				UtilsFile.copyFile(srcFile,targetFile);
				imagePath=targetFile.nativePath.replace(mapDic,"");
			}else{
				imagePath=srcFile.nativePath.replace(mapDic,"");
			}
			
			var oldPropertys:Vector.<PropertyChangeData>=new Vector.<PropertyChangeData>();
			oldPropertys.push(new PropertyChangeData(this.property.GetKey(),this.property.GetValue()));
			
			var newPropertys:Vector.<PropertyChangeData>=new Vector.<PropertyChangeData>();
			newPropertys.push(new PropertyChangeData(this.property.GetKey(),imagePath));
			
			this.project.undoSystem.executeOperation(Operations.COLLECTION_PROPERTYS_CHANGE,{parent:this.data,newPropertys:newPropertys,oldPropertys:oldPropertys});
		}
		
		private function PropertyUpdateHandler(e:ModelEvent):void{
			this.RefreshView();
		}
	}
}